/*
=======================================================================

  BOTS Console Commands - game

=======================================================================
*/
#include "g_local.h"
#include "bots_local.h"
#include "bots_captain.h"
#include "bots_scientist.h"
#include "bots_bodyguard.h"
#include "bots_sniper.h"
#include "bots_soldier.h"
#include "bots_berzerker.h"
#include "bots_infiltrator.h"
#include "bots_kamikazee.h"
#include "bots_nurse.h"
#include "bots_ref.h"

// NITS - test
// used to test menus
char* playerListTest[] = 
{
	{"player1"},
	{"player2"},
	{"player3"},
	{"player4"},
	{"player5"},
	{"player6"},
	{"player7"},
	{"player8"},
	{"player9"},
	{"player10"},
	{"player11"},
	{"player12"},
	{"player13"},
	{"player14"},
	{"player15"},
	{"player16"},
	{"player17"}
};
int numPlayersTest = 17;
// NITS - test

int BOTS_Get_clientnum_from_name(char* name)
{
	gentity_t* player;
	int i;
	for ( i = 0; i < level.maxclients ; i++ )
	{
		player = g_entities + i;

		if (Q_stricmp (name, player->client->pers.netname) == 0)
			return i;
	}

	return -1;
}

gentity_t* BOTS_Get_gentity_from_name(char* name)
{
	gentity_t* player;
	int i;

	for ( i = 0; i < level.maxclients ; i++ )
	{
		player = g_entities + i;

		if (Q_stricmp (name, player->client->pers.netname) == 0)
			return player;
	}

	return NULL;
}

void Cmd_Kill_f( gentity_t *ent );

qboolean BOTS_ClientCommand(int clientNum)
{
	char	cmd[MAX_TOKEN_CHARS];
	char	fromUI[MAX_TOKEN_CHARS];
	char	list[MAX_INFO_STRING];
	int		i;


	gentity_t*	ent;

	ent = g_entities + clientNum;

	// get the string
	trap_Argv( 0, cmd, sizeof( cmd ) );
	
	// trap_GetConfigstring(CS_PLAYERS+clientNum, netname, sizeof(netname));
	// Com_Printf("netname = %s\n", Info_ValueForKey(netname, "n"));

	// get any passed parameters
	if (trap_Argc() > 1)
		trap_Argv( 1, fromUI, sizeof( fromUI ) );


// BEGIN bots - tbp
	// Team/Class debug
	if (Q_stricmp (cmd, "botsinfo") == 0) {
		trap_SendServerCommand( clientNum, va("print \"team %i bots_team %i class %i\n\"",
					ent->client->sess.sessionTeam,
					ent->bots_team,
					ent->bots_class
					));
		return qtrue;
	}
// END bots - tbp
	else if (Q_stricmp (cmd, "captain") == 0) {
		if (ent->bots_class != BOTS_CAPTAIN) {
			// New rule, if there's less than 6 players no team can have both a captain *and* a scientist
			if ((bots_getGlobalCount() < 6) && (bots_getScientist(ent->bots_team) != NULL)) {
				BOTS_cp_console(ent->s.clientNum, "This game is too small and your team already has a scientist.");
				bots_ChangeClass ( ent, BOTS_BODYGUARD );
			}
			else {
				// check to see if there already is a captain
				if (!bots_getCaptain(ent->bots_team))
					bots_ChangeClass ( ent, BOTS_CAPTAIN );
				else {
					BOTS_cp_console(ent->s.clientNum, "Your team already has a Captain.");
					bots_ChangeClass ( ent, BOTS_BODYGUARD );
				}
			}
		}
		return qtrue;
	}
	else if (Q_stricmp (cmd, "bodyguard") == 0)
	{
		bots_ChangeClass ( ent, BOTS_BODYGUARD );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "sniper") == 0)
	{
		bots_ChangeClass ( ent, BOTS_SNIPER );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "soldier") == 0)
	{
		bots_ChangeClass ( ent, BOTS_SOLDIER );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "berzerker") == 0)
	{
		bots_ChangeClass ( ent, BOTS_BERZERKER );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "infiltrator") == 0)
	{
		bots_ChangeClass ( ent, BOTS_INFILTRATOR );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "kamikazee") == 0)
	{
		bots_ChangeClass ( ent, BOTS_KAMIKAZEE );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "nurse") == 0)
	{
		bots_ChangeClass ( ent, BOTS_NURSE );
		return qtrue;
	}
	else if (Q_stricmp (cmd, "scientist") == 0)
	{
		if (ent->bots_class != BOTS_SCIENTIST) {
			// New rule, if there's less than 6 players no team can have both a captain *and* a scientist
			if ((bots_getGlobalCount() < 6) && (bots_getCaptain(ent->bots_team) != NULL)) {
				BOTS_cp_console(ent->s.clientNum, "This game is too small and your team already has a captain.");
				bots_ChangeClass ( ent, BOTS_BODYGUARD );
			}
			else {
				if (!bots_getScientist(ent->bots_team))
					bots_ChangeClass ( ent, BOTS_SCIENTIST );
				else {
					BOTS_cp_console(ent->s.clientNum, "Your team already has a Scientist.");
					bots_ChangeClass ( ent, BOTS_BODYGUARD );
				}
			}
		}
		return qtrue;
	}

	else if (Q_strncmp (cmd, "mapvote", strlen ("mapvote")) == 0)
	{
		bots_voteSetup(clientNum);
		return qtrue;
	}

	// Radio
	else if (Q_strncmp (cmd, "radio_broadcast", strlen ("radio_broadcast")) == 0)
	{
		BOTS_radio(ent, fromUI);	
		return qtrue;
	}
/////////////////////////////////////////////
// BOTS client commands that are issued to set up a data list
// for a special menu.  Once the list is created we pass a server
// command so that the cgame code can call to the UI to display a menu.
//
// NITS:FIXME -	I am passing in dummy lists for testing purposes.
//				We need to create real lists after Spinoza's code is in.

	else if (Q_strncmp (cmd, "rejoin", strlen ("rejoin")) == 0)
	{
		Cmd_Kill_f (ent);
		trap_SendServerCommand(clientNum, "rejoin_data_complete");
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_rejoin_menu", strlen ("UI_to_server_rejoin_menu")) == 0 )
	{
		bots_ChangeTeam(ent, fromUI);
		return qtrue;
	}

	
/////////////////////////////////////////////
// SPECTATOR'S CAN NOT USE THE FOLLOWING COMMANDS
	if (ent->client->sess.sessionTeam == TEAM_SPECTATOR ||
		ent->client->sess.sessionTeam == TEAM_FREE		||
		ent->health <= 0)
		return qtrue;
/////////////////////////////////////////////
	
	if (Q_stricmp (cmd, "togscout") == 0)
	{
		if (ent->client->pers.scoutmsg) {
			ent->client->pers.scoutmsg = qfalse;
			BOTS_cp_console(ent->s.clientNum, "Captain scout message has been disabled.");
		}
		else {
			ent->client->pers.scoutmsg = qtrue;
			BOTS_cp_console(ent->s.clientNum, "Captain scout message has been enabled.");
		}
		return qtrue;
	}
	else if (Q_stricmp (cmd, "togtech") == 0)
	{
		if (ent->client->pers.techmsg) {
			ent->client->pers.techmsg = qfalse;
			BOTS_cp_console(ent->s.clientNum, "Scientist tech message has been disabled.");
		}
		else {
			ent->client->pers.techmsg = qtrue;
			BOTS_cp_console(ent->s.clientNum, "Scientist tech message has been enabled.");
		}
		return qtrue;
	}
	else if (Q_stricmp (cmd, "togalarm") == 0)
	{
		if (ent->client->pers.alarmmsg) {
			ent->client->pers.alarmmsg = qfalse;
			BOTS_cp_console(ent->s.clientNum, "Sniper alarm message has been disabled.");
		}
		else {
			ent->client->pers.alarmmsg = qtrue;
			BOTS_cp_console(ent->s.clientNum, "Sniper alarm message has been enabled.");
		}
		return qtrue;
	}
	else if (Q_strncmp (cmd, "create", strlen ("create")) == 0)
	{
		if (ent->bots_class == BOTS_NURSE)
			BOTS_nurseCreate_setup(clientNum);
		else if (ent->bots_class == BOTS_SCIENTIST)
			BOTS_scientistCreate_setup(clientNum);
		else
			BOTS_cp_console(clientNum, "Only the Nurse or Scientist can create.");

		//trap_SendServerCommand(clientNum, "create_data_complete");
		return qtrue;
	}
	else if (Q_strncmp (cmd, "genresearch", strlen ("genresearch")) == 0)
	{
		if (ent->bots_class == BOTS_SCIENTIST)
			BOTS_scientistResearch_setup(clientNum);
		else
			BOTS_cp_console(clientNum, "Only the Scientist can research.");

//		trap_SendServerCommand(clientNum, "genresearch_data_complete");
		return qtrue;
	}

	/////////////////////////////////////////////

	else if (Q_strncmp (cmd, "vote", strlen ("vote")) == 0)
	{
		for (i = 0; i < numPlayersTest; i++)
			strcat(list, va("\\name%i\\%s", i, playerListTest[i]));
		trap_SetConfigstring(CS_BOTS_MENU + clientNum, va("size\\%i%s", numPlayersTest, list));

		trap_SendServerCommand(clientNum, "vote_list_complete");
		return qtrue;
	}
	else if (Q_strncmp (cmd, "mute", strlen ("mute")) == 0)
	{
		gentity_t*	player;
		int			count;
		char*		list = va("");
		
		// construct the list of names
		for ( i = 0, count = 0 ; i < level.maxclients ; i++ )
		{
			qboolean	include_player = qtrue;
			int			ii;

			player = g_entities + i;

			// do not include muted players
			for (ii = 0; ii < BOTS_MUTE_CAPACITY; ii++)
			{
				if (player == ent->mute[ii])
					include_player = qfalse;
			}
			if (!include_player)
				continue;
			if (!player->inuse || !player->client)
				continue; 
			if (player == ent)
				continue;

			list = va("%s\\name%i\\%i", list, count, player->s.clientNum);
			count++;
		}

		// set the list in the config string
		trap_SetConfigstring(CS_BOTS_MENU + clientNum, va("size\\%i%s", count, list));

		// send us back to cgame
		trap_SendServerCommand(clientNum, "mute_list_complete");
		return qtrue;
	}
	else if (Q_strncmp (cmd, "unmute", strlen ("unmute")) == 0)
	{
		int		count = 0;
		char*	list = va("");

		// loop through the mute list and record the names of players
		// muted into the config string.
		for (i = 0; i < BOTS_MUTE_CAPACITY; i++)
		{
			if (ent->mute[i])
			{
				list = va("%s\\name%i\\%i", list, count, ent->mute[i]->s.clientNum);
				count++;
			}
		}

		// set the list in the config string
		trap_SetConfigstring(CS_BOTS_MENU + clientNum, va("size\\%i%s", count, list));

		// send us back to cgame
		trap_SendServerCommand(clientNum, "unmute_list_complete");
		return qtrue;
	}

/////////////////////////////////////////////
// BOTS client commands that are issued from the UI menu picks
// These commands are sent to the server from the UI.  The server must
// have the correct logic to parse the command and extract the arguments
// passed (argv/argc).
//
	else if (Q_strncmp (cmd, "UI_to_server_create", strlen ("UI_to_server_create")) == 0 )
	{
		if (ent->bots_class == BOTS_NURSE)
			BOTS_nurseCreate_item(clientNum, fromUI);
		else
			BOTS_scientistCreate_item(clientNum, fromUI);

		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_vote", strlen ("UI_to_server_vote")) == 0 )
	{
		trap_SendServerCommand( clientNum, va("cp \"vote -> %s\n\"", fromUI));
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_mapvote", strlen ("UI_to_server_mapvote")) == 0 )
	{
		bots_mapVote( clientNum, fromUI);
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_mute", strlen ("UI_to_server_mute")) == 0 )
	{
		int			j;
		char		netname[30];
		gentity_t*	player = g_entities + atoi(fromUI);

		// run through the mute list and set the player to mute
		for (j = 0; j < BOTS_MUTE_CAPACITY; j++)
		{
			if (ent->mute[j])
				continue;
			else
			{
				ent->mute[j] = player;
				j = BOTS_MUTE_CAPACITY;
			}
		}

		// remove the stupid ass colors losers use
		strcpy(netname, player->client->pers.netname);
		Q_CleanStr(netname);

		BOTS_cp_console(clientNum, va("You have muted %s", netname));
		BOTS_cp_console(player->s.clientNum,
						va("You have been muted by %s", ent->client->pers.netname));
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_unmute", strlen ("UI_to_server_unmute")) == 0 )
	{
		int			j;
		char		netname[30];
		gentity_t*	player = g_entities + atoi(fromUI);
		
		// run through the mute list and remove the player from it
		for (j = 0; j < BOTS_MUTE_CAPACITY; j++)
		{
			if (ent->mute[j] == player)
			{
				ent->mute[j] = NULL;
				j = BOTS_MUTE_CAPACITY;
			}
		}

		// remove the stupid ass colors losers use
		strcpy(netname, player->client->pers.netname);
		Q_CleanStr(netname);
		
		BOTS_cp_console(clientNum, va("You have unmuted %s", netname));
		BOTS_cp_console(player->s.clientNum, 
						va("You have been unmuted by %s", ent->client->pers.netname));

		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_disguise", strlen ("UI_to_server_disguise")) == 0 )
	{
		BOTS_disguise(clientNum);
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_genresearch", strlen ("UI_to_server_genresearch")) == 0 )
	{
		if (ent->bots_class == BOTS_SCIENTIST) {
			BOTS_setResearch(clientNum, fromUI);
			BOTS_scientistResearchClass_setup(clientNum);
		}
		else
			BOTS_cp_console(clientNum, "Only the Scientist can research.");
		return qtrue;
	}
	else if (Q_strncmp (cmd, "UI_to_server_classresearch", strlen ("UI_to_server_classresearch")) == 0 )
	{
		if (ent->bots_class == BOTS_SCIENTIST)
			BOTS_scientistResearchClass(clientNum, fromUI);
		else
			BOTS_cp_console(clientNum, "Only the Scientist can research.");
		return qtrue;
	}

/////////////////////////////////////////////
// Class specific commands
//
	else if (BOTS_CaptainCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_BodyguardCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_SniperCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_SoldierCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_BerzerkerCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_InfiltratorCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_KamikazeeCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_NurseCommands(clientNum, cmd))
		return qtrue;
	else if (BOTS_ScientistCommands(clientNum, cmd))
		return qtrue;

	else if (BOTS_ClassGeneralCommands(clientNum, cmd))
		return qtrue;

	return qfalse;
}
